package com.ruoyi.web.controller.system.fuw.wx;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fuw.domain.FuwComm;
import com.ruoyi.fuw.service.IFuwCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Wx商品信息Controller
 * 
 * @author zjh
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/api/wx/comm")
public class WxFuwCommController extends BaseController
{
    @Autowired
    private IFuwCommService fuwCommService;

    /**
     * 查询商城目录信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:query')")
    @GetMapping(value = "/list/dic")
    public AjaxResult getCommDic()
    {
        // 查询每种商品一件，作为分类的样板
        List<FuwComm> commList = fuwCommService.selectFuwCommList(null);

        // 筛选一样一条
        List<FuwComm> commListNew = commList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCommType()))),
                        ArrayList::new));

        // 过滤掉正在服务状态的岗位组别
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> listMapSon1 = new ArrayList<Map<String,Object>>(); // 诊断检测类
        List<Map<String,Object>> listMapSon2 = new ArrayList<Map<String,Object>>(); // 治疗器具类
        List<Map<String,Object>> listMapSon3 = new ArrayList<Map<String,Object>>(); // 保健康复类
        for(FuwComm fuwComm:commListNew){
            if(fuwComm.getCommType().split(",")[0].equals("诊断检测类")){
                Map<String, Object> mapNew = new HashMap<>();
                mapNew.put("name", fuwComm.getCommType().split(",")[1]);
                mapNew.put("img", fuwComm.getCommPicture());
                listMapSon1.add(mapNew);
            }else if(fuwComm.getCommType().split(",")[0].equals("治疗器具类")){
                Map<String, Object> mapNew = new HashMap<>();
                mapNew.put("name", fuwComm.getCommType().split(",")[1]);
                mapNew.put("img", fuwComm.getCommPicture());
                listMapSon2.add(mapNew);
            }else if(fuwComm.getCommType().split(",")[0].equals("保健康复类")){
                Map<String, Object> mapNew = new HashMap<>();
                mapNew.put("name", fuwComm.getCommType().split(",")[1]);
                mapNew.put("img", fuwComm.getCommPicture());
                listMapSon3.add(mapNew);
            }
        }
        Map<String, Object> mapNew1 = new HashMap<>(); // 诊断检测类
        mapNew1.put("id", 1);
        mapNew1.put("title", "诊断检测类");
        mapNew1.put("banner", "../../static/img/category/banner.jpg");
        mapNew1.put("list", listMapSon1);
        Map<String, Object> mapNew2 = new HashMap<>(); // 治疗器具类
        mapNew2.put("id", 2);
        mapNew2.put("title", "治疗器具类");
        mapNew2.put("banner", "../../static/img/category/banner.jpg");
        mapNew2.put("list", listMapSon2);
        Map<String, Object> mapNew3 = new HashMap<>(); // 保健康复类
        mapNew3.put("id", 3);
        mapNew3.put("title", "保健康复类");
        mapNew3.put("banner", "../../static/img/category/banner.jpg");
        mapNew3.put("list", listMapSon3);

        listMap.add(mapNew1);
        listMap.add(mapNew2);
        listMap.add(mapNew3);

        return AjaxResult.success(listMap);
    }

    /**
     * 查询对应标签的商品，带分页
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:query')")
    @PostMapping(value = "/list/comm")
    public AjaxResult getCommDicByType(@RequestParam("commName") String commName,
                                       @RequestParam("commType") String commType,
                                       @RequestParam("page") Integer page,
                                       @RequestParam("size") Integer size)
    {
        // 跟进商品标签进行筛选
        FuwComm fuwComm = new FuwComm();
        if(commName != null && commName != ""){
            fuwComm.setCommName(commName);
        }else {
            fuwComm.setCommName(null);
        }
        if(commType != null && commType != ""){
            fuwComm.setCommType(commType);
        }else {
            fuwComm.setCommType(null);
        }
        List<FuwComm> commList = fuwCommService.selectFuwCommWxList(fuwComm, page, size);
        return AjaxResult.success(commList);
    }

    /**
     * 根据商品ID查询商品的详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:comm:query')")
    @GetMapping(value = "/list/commId")
    public AjaxResult getCommById(@RequestParam("commId") String commId)
    {
        FuwComm comm = fuwCommService.selectFuwCommByCommId(commId);
        return AjaxResult.success(comm);
    }

}
